const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');

module.exports = {
  // watch: true,
  mode: "development",
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "./build"),
    // publicPath: './'
    // publicPath: '/abc'
  },
  // 专门为webpack-dev-server
  devServer: {
    hot: true,
    hotOnly: true,
    // publicPath: "/abc",
    watchContentBase: true,
    // host: "0.0.0.0",
    // port: 7777,
    // open: true,
    compress: true,
    // contentBase: path.resolve(__dirname, "./abc") // 第一种解决方案
    contentBase: path.resolve(__dirname, "./yz"), // 第二种解决方案
    proxy: {
      // "/api": "http://localhost:7777",
      "/yz": {
        target: "http://localhost:7777",
        pathRewrite: {
          "^/yz": ""
        },
        secure: false,
        changeOrigin: true
      },
    },
    // historyApiFallback: true,
    historyApiFallback: {
      rewrites: [
        {from: /abc/, to: "index.html" }
      ]
    }
  },
  resolve: {
    extensions: [".wasm", ".mjs", ".js", ".json", ".jsx", ".ts", ".vue"],
    alias: {
      "@": path.resolve(__dirname, "./src"),
      "pages": path.resolve(__dirname, "./src/pages")
    }
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/i,
        use: "babel-loader"
      },
      {
        test: /\.vue$/i,
        use: "vue-loader"
      },
      {
        test: /\.css/i,
        use: [
          "style-loader",
          "css-loader"
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.html"
    }),
    new ReactRefreshWebpackPlugin(),
    new VueLoaderPlugin()
  ]
}